package com.klikmee.sms.cef;

import java.net.URL;
import java.util.Collection;

import javax.xml.ws.BindingProvider;

import org.apache.log4j.Logger;

import com.klikmee.sms.cef.core.ws.BulkPayload;
import com.klikmee.sms.cef.core.ws.CoreInterface;
import com.klikmee.sms.cef.core.ws.CoreInterfaceService;
import com.klikmee.sms.cef.core.ws.ServicePayload;

public class CoreAdapter {

	private static Logger	logger	= Logger.getLogger( CoreAdapter.class );
	private URL				wsdlLocation;
	private CoreInterface	coreInterface;
	private String			coreEndpointURL;

	public void init() {
		// create core ws client
		coreInterface = new CoreInterfaceService( wsdlLocation ).getCoreInterfacePort();
		logger.info( "Core adapter initialized" );
		// override end point address
		((BindingProvider) coreInterface).getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, coreEndpointURL );

		logger.info( String.format( "Core adapter initialized, core URL = [%s]", coreEndpointURL ) );
	}

	public Long sendMessage(Long chargingId, Long serviceId, int contentFlag, Long referenceId, String messageText) {
		// build service payload
		ServicePayload servicePayload = new ServicePayload();
		servicePayload.setContentFlag( contentFlag );
		servicePayload.setCoreReferenceId( referenceId );
		servicePayload.setText( messageText );

		return coreInterface.sendMessage( servicePayload, serviceId, chargingId );
	}
	
	public int sendBulkMessage(Long chargingId, Long serviceId, Collection<? extends String> receivers, String messageText){
		BulkPayload bulkPayload = new BulkPayload();
		bulkPayload.getReceivers().addAll( receivers );
		bulkPayload.setText( messageText );
		
		return coreInterface.sendBulkMessage( bulkPayload, serviceId, chargingId );
	}
	
	public void setCoreEndpointURL(String coreEndPointURL) {
		this.coreEndpointURL = coreEndPointURL;
		
	}
	
	public void setWsdlLocation(URL wsdlLocation) {
		this.wsdlLocation = wsdlLocation;
	}
	
}
